<!doctype html>
<html class="no-js">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Mkoa 通用模型生成器</title>
    <meta name="keywords" content="table">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="icon" type="image/png" href="<%=$STATIC%>i/favicon.png">
    <link rel="apple-touch-icon-precomposed" href="assets/i/app-icon72x72@2x.png">
    <meta name="apple-mobile-web-app-title" content="Amaze UI" />
    <link rel="stylesheet" href="<%=$STATIC%>css/amazeui.min.css"/>
    <link rel="stylesheet" href="<%=$STATIC%>css/admin.css">
    <style>
        .am-modal-bd{overflow-y: scroll;
            height: 373px;}
    </style>
</head>
<body ms-controller="app">
<!--[if lte IE 9]>
<p class="browsehappy">你正在使用<strong>过时</strong>的浏览器， 请 <a href="http://browsehappy.com/" target="_blank">升级浏览器</a>
    以获得更好的体验！</p>
<![endif]-->

<header class="am-topbar admin-header">
    <div class="am-topbar-brand">
        <strong>Mkoa</strong> <small>通用模型生成器</small>
    </div>

    <button class="am-topbar-btn am-topbar-toggle am-btn am-btn-sm am-btn-success am-show-sm-only" data-am-collapse="{target: '#topbar-collapse'}"><span class="am-sr-only">导航切换</span> <span class="am-icon-bars"></span></button>

    <div class="am-collapse am-topbar-collapse" id="topbar-collapse">

        <ul class="am-nav am-nav-pills am-topbar-nav am-topbar-right admin-header-list">
            <li class="am-hide-sm-only"><a href="javascript:;" id="admin-fullscreen"><span class="am-icon-arrows-alt"></span> <span class="admin-fullText">开启全屏</span></a></li>
        </ul>
    </div>
</header>

<div class="am-cf admin-main">
    <!-- sidebar start -->
    <div class="admin-sidebar am-offcanvas" id="admin-offcanvas" ms-include-src="'<%=$HOST%>builder/menu'"></div>
    <!-- sidebar end -->

    <!-- content start -->
    <div class="admin-content">

        <div class="am-cf am-padding">
            <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">字段</strong> / <small>模型字段</small></div>
            <div class="am-fr"> <button type="button" class="am-btn am-btn-primary am-btn-sm" data-am-modal="{target: '#doc-modal-1', closeViaDimmer: 0, width: 750, height: 425}"><span class="am-icon-plus"></span> 增加字段</button></div>
        </div>

        <div class="am-g">

        </div>

        <div class="am-g">
            <div class="am-u-sm-12">
                <form class="am-form">
                    <table class="am-table am-table-striped am-table-hover table-main">
                        <thead>
                        <tr>
                            <th>ID</th><th>字段名</th><th>字段描述</th><th>字段类型</th><th>是否为空</th><th>是否唯一</th><th>默认值</th><th>验证规则</th><th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr ms-repeat="fields">
                            <td>{{$index}}</td>
                            <td>{{el.name}}</td>
                            <td>{{el.comment}}</td>
                            <td>{{el.type}}</td>
                            <td>{{el.allowNull}}</td>
                            <td>{{el.unique}}</td>
                            <td>{{el.defaultValue}}</td>
                            <td>['{{el.validate.rule}}','{{el.validate.error}}']</td>
                            <td>
                                <div class="am-btn-toolbar">
                                    <div class="am-btn-group am-btn-group-xs">
                                        <button class="am-btn am-btn-default am-btn-xs am-hide-sm-only" type="button" ms-click="up($index)">上移</button>
                                        <button class="am-btn am-btn-default am-btn-xs am-hide-sm-only"  type="button" ms-click="down($index)">下移</button>
                                        <button class="am-btn am-btn-default am-btn-xs am-text-secondary" type="button" ms-click="editor($index)"><span class="am-icon-pencil-square-o"></span> 编辑</button>
                                        <button class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only" type="button" ms-click="$remove"><span class="am-icon-trash-o"></span> 删除</button>
                                        </div>
                                </div>
                            </td>
                        </tr>
                        </tbody>
                    </table>

                </form>
            </div>
            <div class="am-margin">
                <form class="am-form am-form-horizontal am-input-sm">
                    <div class="am-form-group">
                        <label for="doc-ipt-3" class="am-u-sm-2 am-form-label">模型名称/表名</label>
                        <div class="am-u-sm-10">
                            <input type="text" id="doc-ipt-3" ms-duplex="modelName" placeholder="小写英文开头：驼峰写法">
                        </div>
                    </div>

                    <div class="am-form-group">
                        <label for="doc-ipt-pwd-2" class="am-u-sm-2 am-form-label">描述</label>
                        <div class="am-u-sm-10">
                            <input type="text" id="doc-ipt-pwd-2" ms-duplex="comment" placeholder="中文描述">
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label class="am-u-sm-2 am-form-label">增加记录字段</label>
                        <div class="am-u-sm-7">
                            <select ms-duplex="timestamps">
                                <option value="false">否</option>
                                <option value="true">是</option>
                            </select>
                        </div> <div class="am-u-sm-3">updatedAt, createdAt</div>
                    </div>
                    <div class="am-form-group" ms-if="timestamps==1">
                        <label class="am-u-sm-2 am-form-label">不删除数据</label>
                        <div class="am-u-sm-7">
                            <select ms-duplex="paranoid">
                                <option value="false">否</option>
                                <option value="true">是</option>
                            </select>
                        </div> <div class="am-u-sm-3 am-text-xs"></div>
                    </div>
                    <div class="am-form-group">
                        <label  class="am-u-sm-2 am-form-label">索引配置</label>
                        <div class="am-u-sm-10">
                            <textarea ms-duplex="indexes" id="" cols="30" rows="3"></textarea>
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label class="am-u-sm-2 am-form-label">文件模块目录</label>
                        <div class="am-u-sm-10">
                            <input type="text" ms-duplex="root" >
                        </div>
                    </div>
                    <div class="am-form-group">
                        <label class="am-u-sm-2 am-form-label">控制器存放目录</label>
                        <div class="am-u-sm-10">
                            <input type="text" ms-duplex="filepath" >
                        </div>
                    </div>
                    <div class="am-form-group" >
                        <label class="am-u-sm-2 am-form-label">是否生成案例视图</label>
                        <div class="am-u-sm-7">
                            <select ms-duplex-boolean="buildViews">
                                <option value="false">否</option>
                                <option value="true">是</option>
                            </select>
                        </div> <div class="am-u-sm-3 am-text-xs"></div>
                    </div>
                    <div class="am-form-group">
                        <div class="am-u-sm-10 am-u-sm-offset-2">
                            <button type="button" ms-click="save" class="am-btn am-btn-success">生成文件</button>
                            <a ms-if="buildViews==true" ms-href="'<%=$HOST%>builder/admin'"  target="_blank" class="am-btn am-btn-default">生成后点击访问</a>
                        </div>
                    </div>
                </form>

            </div>
        </div>
    </div>
    <!-- content end -->

</div>

<div class="am-modal am-modal-no-btn" tabindex="-1" id="doc-modal-1">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">增加字段
            <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
        </div>
        <div class="am-modal-bd">
            <div ms-include-src="'<%=$HOST%>builder/add'" class="am-margin-top"></div>
        </div>
    </div>
</div>
<div class="am-modal am-modal-no-btn" tabindex="-1" id="doc-modal-2">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">修改
            <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
        </div>
        <div class="am-modal-bd">
            <div ms-include-src="'<%=$HOST%>builder/editor'" class="am-margin-top"></div>
        </div>
    </div>
</div>
<footer>
    <hr>
    <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
</footer>

<!--[if lt IE 9]>
<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.staticfile.org/modernizr/2.8.3/modernizr.js"></script>
<script src="<%=$STATIC%>js/amazeui.ie8polyfill.min.js"></script>
<![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<script src="<%=$STATIC%>js/jquery.min.js"></script>
<!--<![endif]-->
<script src="<%=$STATIC%>js/amazeui.min.js"></script>
<script src="<%=$STATIC%>js/app.js"></script>
<script src="<%=$STATIC%>avalon.min.js"></script>
<script>
    window.extend=function(obj,newobj,isReset){//替换存在属性
        avalon.each(obj,function(key){
            if(newobj[key]==0||newobj[key]){
                if(avalon.isPlainObject(obj[key])){
                    if(avalon.isPlainObject(newobj[key])&&!isEmpty(newobj[key])){
                     console.log(obj[key],newobj[key])
                        extend(obj[key],newobj[key],isReset);
                    }else{
                        obj[key]['value'] = newobj[key];
                    }
                }else{
                    obj[key]=newobj[key];
                }
            }else{
                if(isReset){//重置值
                    obj[key]=isNaN(obj[key])?0:'';
                }
            }
        });
    };
    //判断对象是否为空
    function isEmpty(obj)
    {
        for (var name in obj)
        {
            return false;
        }
        return true;
    }
    var $modal = $('#doc-modal-2');
var lock=false;
    var app=avalon.define({
        $id:'app',
        modelName:"<%-modelName%>",
        comment:"<%-comment%>",
        indexes:"<%-indexes%>",
        buildViews:<%-buildViews%>,
        timestamps:<%-timestamps%>,
        paranoid:<%-paranoid%>,
        fields:<%-JSON.stringify(fields)%>,
        root:'<%-root%>',
         filepath:"<%-filepath%>",
        eindex:0,
        editor:function(index){
            app.eindex=index;
            $modal.modal({width:750,height:422});
        },
        up:function(index){
            var a={};
            avalon.mix(true,a,app.fields[index]);
            var b={};
            avalon.mix(true,b,app.fields[index-1]);
            extend(app.fields[index],b);
            extend(app.fields[index-1],a);
        },
        down:function(index){
            var a={};
            avalon.mix(true,a,app.fields[index]);
            var b={};
            avalon.mix(true,b,app.fields[index+1]);
            extend(app.fields[index],b);
            extend(app.fields[index+1],a);
        },
        save:function() {
            if (!lock) {
                lock = true;
                var postdata = {
                    modelName: app.modelName,
                    comment: app.comment,
                    indexes: app.indexes,
                    buildViews: app.buildViews,
                    timestamps: app.timestamps,
                    paranoid: app.paranoid,
                    root: app.root,
                    filepath:app.filepath,
                    fields: JSON.stringify(app.fields)
                };
                $.post("<%=$HOST%>builder/main/buildModel",//生成模型
                        postdata,
                        function (data) {
                            if (data.error) {
                                alert(data.error)
                            } else {
                                alert('生成模型成功！');
                            }
                        });
                $.post("<%=$HOST%>builder/main/buildController",//生成控制器
                        postdata,
                        function (data) {
                            if (data.error) {
                                alert(data.error)
                            } else {
                                alert('生成控制器成功！');
                            }
                            lock = false;
                        });
                if (app.buildViews) {
                    $.post("<%=$HOST%>builder/main/buildViews",//生成视图
                            postdata,
                            function (data) {
                                if (data.error) {
                                    alert(data.error)
                                } else {
                                    alert('生成视图成功！');
                                }
                                lock = false;
                            });
                }
            }
        }
    });
</script>
</body>
</html>
